/*
 * 文件名： ScanPaySpdbManager.java
 * 
 * 工程名称: bis-manager
 *
 * Gopay
 *
 * 创建日期： 2017年11月9日
 *
 * Copyright(C) 2017, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.manager.bank.scan;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.spdb.agpay.PayGoodsList;
import com.gopay.common.domain.bank.spdb.agpay.ScanPayBody;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.TransUtil;
import com.gopay.remote.bank.businesses.ScanPayService;
import com.ibm.icu.util.Calendar;

/**
 * 扫码支付浦发银行渠道报文拼装处理
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2017年11月9日
 */
@Service("scanPaySpdbManager")
public class ScanPaySpdbManager {

	@Resource(name = "agPaySpdbPayConfig")
	private Map<String, String> agPaySpdbPayConfig;

	@Resource(name = "agPaySpdbServiceClient")
	private ScanPayService agPaySpdbServiceClient;

	public Object payment(BaseMessage baseMsg) throws GopayException {
		baseMsg.setBankOrderId(modifyOrderId(baseMsg.getOrderId()));
		
		ScanPayBody body = new ScanPayBody();
		PayGoodsList goodsMessage = new PayGoodsList();
		BeanUtils.copyProperties(baseMsg, body);

		body.setVersion(agPaySpdbPayConfig.get("SPDB.AGPAY.VERSION"));
		body.setSignType(agPaySpdbPayConfig.get("SPDB.AGPAY.SIGNTYPE"));
		body.setCharset(agPaySpdbPayConfig.get("SPDB.AGPAY.CHARSET"));
		body.setTxndir("Q");// 交易方向，Q为请求，A为应答
		body.setBusicd(agPaySpdbPayConfig.get("SPDB.AGPAY.SCANPAY.BUSICD"));
		body.setInscd(agPaySpdbPayConfig.get("SPDB.AGPAY.INSCD"));// 机构号
		String chcd=null;
		if(BankConstants.BANK_LOGO_ALIPAY.equals(baseMsg.getBank())){
			chcd="ALP";
		}
		if(BankConstants.BANK_LOGO_WECHAT.equals(baseMsg.getBank())){
			chcd="WXP";
		}
		body.setChcd(chcd);//渠道
		body.setMchntid(baseMsg.getMerID());
		body.setTerminalid(agPaySpdbPayConfig.get("SPDB.AGPAY.TERMINALID"));// 终端号
		
		body.setOrderNum(baseMsg.getBankOrderId());
		body.setTxamt(TransUtil.amountConvert2Fen(String.valueOf(baseMsg.getAmount()), 12, "0"));
		body.setCurrency("CNY");

		goodsMessage.setGoodsName(body.getGoodsBody());
		goodsMessage.setPrice(TransUtil.amountConvert2Fen(String.valueOf(baseMsg.getAmount())));// 精确到分不需要补位
		goodsMessage.setGoodsNum("1");// 数量
		goodsMessage.setGoodsId(body.getGoodsBody());// 商品条码
		
		List<PayGoodsList> list = new ArrayList<PayGoodsList>();
		list.add(goodsMessage);
		body.setGoodsList(JSON.toJSONString(list));

		body.setOutStlmCd(body.getBank());
		body.setBackUrl(baseMsg.getPayRuturnUrl());
		
		body.setMchntid(baseMsg.getMerID());
		body.setSignKey(baseMsg.getPrivateKeyPath());
		body.setTimeStart(DateUtils.format(new Date()));
		body.setTimeExpire(DateUtils.format(DateUtils.add(DateUtils.strToDate(body.getTimeStart()), Calendar.MINUTE, 6)));
		BankLogUtils.printWeChatSdkManagerPaySendLog(body);
		return agPaySpdbServiceClient.payment(body);
	}

	/**
	 * @Description: 替换国付宝订单号前2位为SP
	 * @param orderId
	 * @return
	 * @author jxj
	 */
	private static String modifyOrderId(String orderId) {
		return "SP" + orderId.substring(2);
	}
}
